	//
	//  GaoTextView.m
	//  UgcPlay
	//
	//  Created by Gikki Ares on 2021/4/23.
	//  Copyright © 2021 Tianma. All rights reserved.
	//

#import "JsTextView.h"

#import "JsGeometryManager.h"


@interface JsTextViewVm()

@property(nonatomic,assign) UIEdgeInsets textViewInset;
@property(nonatomic,assign) UIEdgeInsets placeholderInset;



@end

@implementation JsTextViewVm

- (instancetype)init {
	if(self = [super init]) {
		self.textViewInset = UIEdgeInsetsMake(9, 5, 9, 5);
		self.placeholderInset = UIEdgeInsetsMake(0, 5, 0, 5);
		self.contentFont = [UIFont systemFontOfSize:15];
	}
	return self;
}


- (void)setContent:(NSString *)content {
	_content = content;
		//	[self doCalculate];
}

- (NSString *)placeholder {
	if(!_placeholder.length) {
		return @"有爱评论,说点儿好听的~";
	}
	return _placeholder;
}

- (BOOL)isEmpty {
	if(self.content.length == 0) {
		return YES;
	}
	else {
		return NO;
	}
}


	//- (void)setFrame:(CGRect)frame {
	//	_frame = frame;
	//	[self doCalculate];
	//}

	//计算子视图的位置.根据内容,计算尺寸
- (void)doCalculate {
	
	self.emptyHeight = [JsGeometryManager sizeForOneLineStringWithFont:self.contentFont fixedWidth:100].height + self.textViewInset.top + self.textViewInset.bottom ;
	
		// |-textViewInsets.left-textView-textViewInsets.right-|
	float contentMaxWidth = self.initalWidth - self.textViewInset.left - self.textViewInset.right;
	
		//TextView的size
	CGSize size = [JsGeometryManager sizeForMultiLineStringWithContext:self.content font:self.contentFont maxWidth:contentMaxWidth isWidthFixed:YES maxHeight:self.maxHeight minHeight:0];
	
	self.computedSize = CGSizeMake(self.initalWidth, size.height + self.textViewInset.top + self.textViewInset.bottom);
	
	self.textViewFrame = [JsGeometryManager frameHAlignCenterVAlignCenterWithContainerSize:self.computedSize size:size];
		//TODO: 同样大小的label和textview,textview的光标出现的问题并不是0?
	self.placeholderFrame = [JsGeometryManager frameWithInsets:self.placeholderInset inSiblingFrame:self.textViewFrame];
	
		//计算出来的contentSize,+textViewInsets.top+textViewInsets.bottom是整个的计算出来的size.
	
}

@end



@interface JsTextView() {
	UILabel * mlb_placeholder;
	UITextView * mtv;
	UIButton * mbtn;
}

@end

@implementation JsTextView

- (instancetype)initWithFrame:(CGRect)frame {
	if(self = [super initWithFrame:frame]) {
		[self commonInit];
	}
	return self;
}

//初始化时自动创建vm,然后也能设置新的vm.
- (void)commonInit {
	_vm = [JsTextViewVm new];
	[self setupUi];
//	UIView * inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 300)];
//	inputView.backgroundColor = [UIColor redColor];
//	[self.textView setInputView:inputView];
}

- (void)setVm:(JsTextViewVm *)vm {
	_vm = vm;
	[self setupUi];
}

- (UITextView *)textView {
	return mtv;
}

- (void)setPlaceholder:(NSString *)placeholder {
	_vm.placeholder = placeholder;
	[_vm doCalculate];
	[self setupUi];
}

	//vm已经计算好了frame,updateUi中只需要设置.
//updateUi 关于自己只修改size,frame单独修改.
- (void)setupUi {
		//颜色为可变的.
	self.backgroundColor = self.vm.backgroundColor;
	self.layer.cornerRadius = _vm.emptyHeight/2;
	self.layer.masksToBounds = YES;
	
	
	{
		if(!mtv) {
			mtv = [[UITextView alloc]initWithFrame:_vm.textViewFrame];
			[self addSubview:mtv];
			mtv.backgroundColor = [UIColor clearColor];
			mtv.textContainerInset = UIEdgeInsetsZero;
		}
		mtv.textColor = self.vm.contentColor;
		mtv.font = _vm.contentFont;
		
		//MARK: 输入法本身,就会更新输入框的text,不需要再设置.
//		mtv.text = self.vm.content;
		mtv.frame = _vm.textViewFrame;
		[mtv scrollRangeToVisible:NSMakeRange(self.vm.content.length-1, 1)];
		
	}
	
	{
		if(!mlb_placeholder) {
			mlb_placeholder = [UILabel new];
			mlb_placeholder.backgroundColor = [UIColor clearColor];
			[self addSubview:mlb_placeholder];
			mlb_placeholder.frame = _vm.placeholderFrame;
			mlb_placeholder.text = _vm.placeholder;
			mlb_placeholder.font = _vm.contentFont;
			
			
		}
		if(self.vm.isEmpty) {
			mlb_placeholder.hidden = NO;
			mlb_placeholder.frame = _vm.placeholderFrame;
			mlb_placeholder.textColor = _vm.contentColor;
			mlb_placeholder.text = _vm.placeholder;
		}
		else {
			mlb_placeholder.hidden = YES;
		}
	}
	
	//不能加button
//	{
//		if(!mbtn) {
//			mbtn = [[UIButton alloc]initWithFrame:self.bounds];
//			[mbtn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
//			[self addSubview:mbtn];
//		}
//		mbtn.frame = self.bounds;
//	}
	
}

	//frame发生变动后,应该重新子元素.
	//layoutSubview可以使用吗?
	//- (void)setFrame:(CGRect)frame {
	//	[super setFrame:frame];
	//	if(!CGRectEqualToRect(_vm.frame, frame)) {
	//		_vm.frame = frame;
	//	}
	//}

- (void)onClick {
	if(_delegate && [_delegate respondsToSelector:@selector(jsTextViewClick:)]) {
		[_delegate jsTextViewClick:self];
	}
}

@end
